﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using QSHC.CustomControl.Windows;
using QSHC.LibrarySL.MVVM;
using QSHC.QSchedule.Silverlight.Helpers;
using QSHC.QSchedule.Silverlight.Models;
using QSHC.QSchedule.Silverlight.Views;
using QSHC.QSchedule.Web.Models;
using QSHC.QSchedule.Web.Services;

namespace QSHC.QSchedule.Silverlight.ViewModels
{
    public class PhysicianScheduleViewModel : ViewModelBase
    {
        private QScheduleDomainContext _dc = new QScheduleDomainContext();

        #region Property Physicians
        private IEnumerable<Employee> _physicians;
        public IEnumerable<Employee> Physicians
        {
            get { return _physicians; }
            set
            {
                if (_physicians != value)
                {
                    _physicians = value;
                    OnPropertyChanged(() => Physicians);
                }
            }
        }
        #endregion

        #region Property Services
        private IEnumerable<Service> _services;
        public IEnumerable<Service> Services
        {
            get { return _services; }
            set
            {
                if (_services != value)
                {
                    _services = value;
                    OnPropertyChanged(() => Services);
                }
            }
        }
        #endregion

        #region Property SelectedPhysician
        private Employee _selectedPhysician;
        public Employee SelectedPhysician
        {
            get { return _selectedPhysician; }
            set
            {
                if (_selectedPhysician != value)
                {
                    _selectedPhysician = value;
                    OnPropertyChanged(() => SelectedPhysician);

                    AddCommand.OnCanExecuteChanged();
                    LoadPhysicianSchedules();
                }
            }
        }
        #endregion

        #region Property PhysicianSchedules
        private IEnumerable<PhysicianSchedule> _physicianSchedules;
        public IEnumerable<PhysicianSchedule> PhysicianSchedules
        {
            get { return _physicianSchedules; }
            set
            {
                if (_physicianSchedules != value)
                {
                    _physicianSchedules = value;
                    OnPropertyChanged(() => PhysicianSchedules);
                }
            }
        }
        #endregion

        #region Property SelectedPhysicianSchedule
        private PhysicianSchedule _selectedPhysicianSchedule;
        public PhysicianSchedule SelectedPhysicianSchedule
        {
            get { return _selectedPhysicianSchedule; }
            set
            {
                if (_selectedPhysicianSchedule != value)
                {
                    _selectedPhysicianSchedule = value;
                    OnPropertyChanged(() => SelectedPhysicianSchedule);

                    EditCommand.OnCanExecuteChanged();
                    DeleteCommand.OnCanExecuteChanged();
                }
            }
        }
        #endregion

        #region AddCommand
        private ViewModelCommand _addCommand = null;
        public ViewModelCommand AddCommand
        {
            get
            {
                if (_addCommand == null)
                    _addCommand = new ViewModelCommand(p => Add(p), p => CanAdd);
                return _addCommand;
            }
        }

        private void Add(object obj)
        {
            AddPhysicianSchedule();
        }
        
        public bool CanAdd
        {
            get { return SelectedPhysician != null; }
        }
        #endregion

        #region EditCommand
        private ViewModelCommand _editCommand = null;
        public ViewModelCommand EditCommand
        {
            get
            {
                if (_editCommand == null)
                    _editCommand = new ViewModelCommand(p => Edit(p), p => CanEdit);
                return _editCommand;
            }
        }

        private void Edit(object obj)
        {
            EditPhysicianSchedule();
        }

        public bool CanEdit
        {
            get { return SelectedPhysicianSchedule != null; }
        }
        #endregion

        #region DeleteCommand
        private ViewModelCommand _deleteCommand = null;
        public ViewModelCommand DeleteCommand
        {
            get
            {
                if (_deleteCommand == null)
                    _deleteCommand = new ViewModelCommand(p => Delete(p), p => CanDelete);
                return _deleteCommand;
            }
        }

        private void Delete(object obj)
        {
            DeletePhysicianSchedule();
        }

        public bool CanDelete
        {
            get { return SelectedPhysicianSchedule != null; }
        }
        #endregion
        
        public PhysicianScheduleViewModel()
        {
            if (!IsInDesignModeStatic)
            {
                Physicians = EmployeeHelper.Physicians;

                var lo = _dc.Load(_dc.GetServicesQuery());
                lo.Completed += (s, e) =>
                {
                    Services = lo.Entities;
                };
            }
        }

        private void LoadPhysicianSchedules()
        {
            var lo = _dc.Load(_dc.GetPhysicianSchedulesByPhysicianCodeQuery(SelectedPhysician.EmployeeCode));
            lo.Completed += (s, e) =>
            {
                PhysicianSchedules = lo.Entities;
            };
        }

        private void AddPhysicianSchedule()
        {
            var vm = new PhysicianScheduleWindowViewModel
            {
                PhysicianSchedule = new PhysicianSchedule 
                                    { 
                                        Physician = SelectedPhysician.EmployeeCode, 
                                        BeginDate = DateTime.Today
                                    },
                Services = Services,
                Quata = 0,
            };

            var cw = new PhysicianScheduleWindow { Title = "เพิ่ม" };
            cw.DataContext = vm;
            cw.Unloaded += (s, e) =>
            {
                if (cw.DialogResult.Value == true)
                {
                    //if (VerifyPhysicianSchedule(vm.PhysicianSchedule.ServiceID, vm.PhysicianSchedule.DayOfWeek))
                    //{
                    var log = new LogPhysicianSchedule
                    {
                        EmployeeCode = EmployeeHelper.LoggedEmployee,
                        LogDateTime = DateTime.Now,
                        Action = 1, // Create
                        PhysicianSchedule = vm.PhysicianSchedule,
                    };

                    _dc.PhysicianSchedules.Add(vm.PhysicianSchedule);
                    _dc.SubmitChanges().Completed += (s2, e2) =>
                    {
                        MessageBox.Show("Saved");
                        LoadPhysicianSchedules();
                    };
                    //}
                    //else
                    //    MessageBox.Show("หน่วยบริการและวันตรวจนี้ได้มีการกำหนดค่าไว้แล้ว");
                }
                SelectedPhysicianSchedule = null;
            };

            cw.Show();
        }

        private void EditPhysicianSchedule()
        {
            var vm = new PhysicianScheduleWindowViewModel
            {
                //PhysicianSchedule = new PhysicianSchedule
                //{
                //    Physician = SelectedPhysician.EmployeeCode,
                //    Location = SelectedPhysicianSchedule.Location,
                //    DayOfWeek = SelectedPhysicianSchedule.DayOfWeek,
                //    Quota = SelectedPhysicianSchedule.Quota,
                //},
                PhysicianSchedule = SelectedPhysicianSchedule,
                Services = Services,
                Quata = 0,
            };

            var cw = new PhysicianScheduleWindow { Title = "แก้ไข" };
            cw.DataContext = vm;
            cw.Unloaded += (s, e) =>
            {
                if (cw.DialogResult.Value == true)
                {
                    //if (VerifyPhysicianSchedule(SelectedPhysicianSchedule.ServiceID, SelectedPhysicianSchedule.DayOfWeek))
                    //{
                    //    //_dc.PhysicianSchedules.Detach(vm.PhysicianSchedule);

                    //    //SelectedPhysicianSchedule.Location = vm.PhysicianSchedule.Location;
                    //    //SelectedPhysicianSchedule.DayOfWeek = vm.PhysicianSchedule.DayOfWeek;
                    //    //SelectedPhysicianSchedule.Quota = vm.PhysicianSchedule.Quota;

                    var Log = new LogPhysicianSchedule
                    {
                        EmployeeCode = EmployeeHelper.LoggedEmployee,
                        LogDateTime = DateTime.Now,
                        Action = 3, // Update
                        PhysicianSchedule = SelectedPhysicianSchedule,
                    };

                    _dc.SubmitChanges().Completed += (s2, e2) =>
                    {
                        MessageBox.Show("Saved");
                        LoadPhysicianSchedules();
                    };
                    //}
                    //else
                    //{
                    //    MessageBox.Show("หน่วยบริการและวันตรวจนี้ได้มีการกำหนดค่าไว้แล้ว");

                    //    _dc.RejectChanges();
                    //    LoadPhysicianSchedules();// ART
                    //}
                }
                else
                {
                    _dc.RejectChanges();
                    LoadPhysicianSchedules();
                }
            };

            cw.Show();
        }

        private void DeletePhysicianSchedule()
        {
            var result = MessageBox.Show("คุณแน่ใจที่จะลบ", "คำเตือน", MessageBoxButton.OKCancel);

            if (result == MessageBoxResult.OK)
            {
                SelectedPhysicianSchedule.IsDeleted = true;

                var log = new LogPhysicianSchedule
                {
                    EmployeeCode = EmployeeHelper.LoggedEmployee,
                    LogDateTime = DateTime.Now,
                    Action = 4, // Delete
                    PhysicianSchedule = SelectedPhysicianSchedule,
                };

                _dc.SubmitChanges().Completed += (s, e) =>
                {
                    MessageBox.Show("Deleted");
                    LoadPhysicianSchedules();
                };
            }
            
        }

        private bool VerifyPhysicianSchedule(int serviceID, int dayOfWeek)
        {
            return PhysicianSchedules.Where(p => p != SelectedPhysicianSchedule &&
                                                 p.ServiceID == serviceID && 
                                                 p.DayOfWeek == dayOfWeek).Count() == 0;
        }
    }
}
